package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MikeCoder
 * @create 2023-12-0421:21
 * @description:
 * @verson:
 */
@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    @Transactional(readOnly = true)
    public PageInfo<SysRole> findByPage(Integer pageNum, Integer pageSize, SysRoleDto sysRoleDto) {
        //1.开启分页
        PageHelper.startPage(pageNum,pageSize);
        //2.查询分页后的数据
        List<SysRole> sysRoleList = sysRoleMapper.selectByRoleName(sysRoleDto.getRoleName());
        //3.生成PageInfo对象
        PageInfo<SysRole> sysRolePageInfo = new PageInfo<>(sysRoleList);
        //4.返回对象
        return sysRolePageInfo;

    }

    @Override
    public Integer add(SysRole sysRole) {
        return sysRoleMapper.insert(sysRole);
    }

    @Override
    public SysRole getSysRoleById(Integer id) {
        return sysRoleMapper.selectSysRoleById(id);
    }

    @Override
    public Integer change(SysRole sysRole) {
        return sysRoleMapper.update(sysRole);
    }

    @Override
    public Integer remove(Integer id) {
        return sysRoleMapper.delete(id);
    }

    @Override
    public Map<String, Object> userRoleInfo(Integer id) {
        //查询所有的角色
        List<SysRole> roleList = sysRoleMapper.selectAllRole();
        //查询用户分配的角色id
        List<Integer> idList = sysRoleMapper.selectUserRoleIds(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userRoleIds",idList);
        map.put("allRoles",roleList);
        return map;
    }

    @Override
    public List<Long> findRoleMenuId(Long roleId) {
        List<Long> menuIds = sysRoleMapper.selectMenuIdByRoleId(roleId);
        return menuIds;
    }
}
